﻿#include "itemview.h"
#include "ui_itemview.h"

ItemView::ItemView(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::ItemView)
{
    ui->setupUi(this);

    base_url=URL_CDN;
    viewType=VT_TEXT;
    isFinished=true;
    defaultSize=this->size();
    manager=new QNetworkAccessManager(this);
    //  connect(manager,&QNetworkAccessManager::finished,this,&ItemView::replyFinished);
}

ItemView::~ItemView()
{
    delete ui;
}

bool ItemView::hasImage() const
{
    QString txt=ui->display->text();
    return txt.isEmpty();
}

void ItemView::downloadUrl(const QString &url)
{
    if(!isFinished)
        return;

    file=new QFile(filePath);
    if(!file->open(QFile::WriteOnly))
    {
        file->deleteLater();
        return;
    }

    isFinished=false;
    downloadReply=manager->get(QNetworkRequest(QUrl(url)));
    connect(downloadReply,&QNetworkReply::readyRead,this,&ItemView::fileReadyRead);
    connect(downloadReply,&QNetworkReply::finished,this,&ItemView::downloadFinish);
}

void ItemView::fileReadyRead()
{
    if(file)
        file->write(downloadReply->readAll());
}

void ItemView::downloadFinish()
{
    file->flush();
    file->close();
    file->deleteLater();
    downloadReply->deleteLater();
    isFinished=true;

    QPixmap pix(filePath);
    if(pix.isNull())
        return;

    ui->display->setPixmap(pix);
}

void ItemView::display(const QString &label,
                       const QString &detail)
{
    if(label.isEmpty() || detail.isEmpty())
        return;

    _title=label;
    setWindowTitle(label);
    resize(defaultSize);
    if(detail.startsWith(PRE_CDN))
    {
        viewType=VT_IMG;
        ui->display->setText(QStringLiteral("正在加载图片，请稍等……"));

        QString fileName=detail;
        fileName=fileName.remove(PRE_CDN);
        filePath=P_FHS_CACHE + fileName;
        QFile file(filePath);
        if(file.exists())
        {
            if(file.size() == 0)
            {
                file.remove();
                ui->display->setText(QStringLiteral("正在更新图片，请稍等……"));
                QString url=base_url + fileName;
                downloadUrl(url);
            }
            else
            {
                QPixmap pix(filePath);
                if(!pix.isNull())
                    ui->display->setPixmap(pix);
            }
        }
        else
        {
            ui->display->setText(QStringLiteral("首次需要下载图片，请稍等……"));
            QString url=base_url + fileName;
            downloadUrl(url);
        }
    }
    else
    {
        viewType=VT_TEXT;
        ui->display->setText(detail);
    }
}

int ItemView::getViewType() const
{
    return viewType;
}

QImage ItemView::getImage() const
{
    QImage img{};
    const QPixmap *pix=ui->display->pixmap();
    if(!pix->isNull())
        img=pix->toImage();
    return img;
}

void ItemView::mouseDoubleClickEvent(QMouseEvent *event)
{
    Q_UNUSED(event)

    QString detail=ui->display->text();
    if(viewType == VT_TEXT)
    {
        if(detail.isEmpty())
            return;

        qApp->clipboard()->setText(detail);
        setWindowTitle(_title + QStringLiteral(" 已复制到剪贴板"));
    }
    else if(viewType == VT_IMG)
    {
        if(!detail.isEmpty())
            return;

        const QPixmap *pix=ui->display->pixmap();
        if(!pix->isNull())
        {
            QImage img=pix->toImage();
            qApp->clipboard()->setImage(img);
            setWindowTitle(QStringLiteral("图片已复制到剪贴板"));
        }
    }
}
